﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Ledict.ConditionExtract.BLL
{
    public class ParagraphToSentenceBll
    {
        /// <summary>
        ///     段分句具体实现
        /// </summary>
        /// <param name="Paragraph"></param>
        /// <param name="RegText"></param>
        /// <param name="ParagraphTypeId"></param>
        /// <param name="returnDic"></param>
        public void GetSentenceByOnePara(string Paragraph, string RegText, int ParagraphTypeId,
            ref List<string> returnDic)
        {
            if (RegText != "")
            {
                var matchs = System.Text.RegularExpressions.Regex.Matches(Paragraph, RegText); //从大段中提取有效段落
                foreach (Match str in matchs)
                {
                    var matchschild = System.Text.RegularExpressions.Regex.Matches(str.ToString(),
                        @"(?<=^|。|\r|\n|；)[^。|；]*?(?:).*?(?:。|；)"); //分句
                    foreach (Match strchild in matchschild)
                    {
                        if (strchild.ToString() != "")
                        {
                            returnDic.Add(strchild.ToString());
                        }
                    }
                }
            }
        }

        /// <summary>
        ///     段分句具体实现
        /// </summary>
        /// <param name="paragraph"></param>
        /// <param name="regText"></param>
        /// <param name="paragraphTypeId"></param>
        /// <param name="returnDic"></param>
        public void GetSentenceNotInOnePara(string paragraph, string regText, int paragraphTypeId,
            ref List<string> returnDic)
        {
            try
            {
                if (regText != "")
                {
                    //MatchCollection matchs = System.Text.RegularExpressions.Regex.Matches(Paragraph, RegText);//从大段中提取有效段落

                    paragraph = System.Text.RegularExpressions.Regex.Replace(paragraph, regText, ""); //从大段中替换符合规则段落

                    var matchschild = System.Text.RegularExpressions.Regex.Matches(paragraph,
                        @"(?<=^|。|\r|\n|；)[^。|；]*?(?:).*?(?:。|；)"); //分句
                    foreach (Match strchild in matchschild)
                    {
                        if (strchild.ToString() != "")
                        {
                            returnDic.Add(strchild.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                throw;
            }
        }


        /// <summary>
        /// 逻辑段分句
        /// </summary>
        /// <param name="paragraph">段</param>
        /// <param name="paragraphTypeId">逻辑段分类</param>
        /// <returns></returns>
        public List<string> GetSentence(string paragraph, int paragraphTypeId)
        {
            //目前只对逻辑段140、150按照规则进行分句，120不分
            var returnDic = new List<string>();
            switch (paragraphTypeId)
            {
                case 140: //正文调查
                    //GetSentenceNotInOnePara(paragraph, @"[^\r]*(诉称|辩称)[^\r]*", 140, ref returnDic); //todo by zhangpeng 按照查询规则（不包含指定内容）进行分句

                    GetSentenceByOnePara(paragraph, @"经(本院)?审理查明.+", 140, ref returnDic);//查明部分

                    break;
                case 150://法院认定
                    var matchschild = System.Text.RegularExpressions.Regex.Matches(paragraph,
                        @"(?<=^|。|\r|\n|；)[^。|；]*?(?:).*?(?:。|；)"); //分句
                    foreach (Match strchild in matchschild)
                    {
                        if (strchild.ToString() != "")
                        {
                            returnDic.Add(strchild.ToString());
                        }
                    }


                    //GetSentenceByOnePara(Paragraph, @".+(?=(判决|裁定)?如下(协议)?[：:])", 150, ref returnDic);//裁判理由
                    break;
                default:
                    returnDic.Add(paragraph);break;
            }
            return returnDic;
        }
    }
}